import Request from 'utils/request';

import BranchForm from 'models/branch/branchForm';
import PartnerForm from 'models/branch/partnerForm';
import MajorTypeForm from 'models/branch/majorTypeForm';

export default class BranchApi {
  static getAllBranch(search, pagination) {
    const apiName = 'api/branch/list';

    let postData = search.getObject(pagination);

    return Request.post(apiName, postData).then(res => {
      let branchs = res.data;
      let branchList = [];
      for (let i = 0; i < branchs.length; i++) {
        let branch = branchs[i];
        branch = new BranchForm(branch);
        branchList.push(branch);
      }

      pagination.totalCount = res.rows;
      return branchList;
    });
  }

  static addBranch(branchForm) {
    const apiName = 'api/branch/save';
    let postData = branchForm.getObject();
    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static updateBranch(branchForm) {
    const apiName = 'api/branch/update';
    let postData = branchForm.getObject();
    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static getAllPartner(search, pagination) {
    const apiName = 'api/partner/list';

    let postData = search.getObject(pagination);

    return Request.post(apiName, postData).then(res => {
      let partners = res.data;
      let partnerList = [];
      for (let i = 0; i < partners.length; i++) {
        let partner = partners[i];
        partner = new PartnerForm(partner);
        partnerList.push(partner);
      }

      pagination.totalCount = res.rows;
      return partnerList;
    });
  }

  static addPartner(partnerForm) {
    const apiName = 'api/partner/save';
    let postData = partnerForm.getObject();
    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static updatePartner(partnerForm) {
    const apiName = 'api/partner/update';
    let postData = partnerForm.getObject();
    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static getMajorList() {
    const apiName = 'api/majortype/all';
    return Request.post(apiName).then(res => {
      return res.data;
    });
  }

  static getBranchList() {
    const apiName = 'api/branch/all';
    return Request.post(apiName, {
      status: 1
    }).then(res => {
      let branchList = res.data;
      branchList = _.map(branchList, branch => {
        return {
          id: branch.branchId,
          name: branch.branchName
        };
      });
      return branchList;
    });
  }

  static getAllMajorList(search,pagination){
    const apiName = 'api/majortype/list';
    let postData = search.getObject(pagination);
    return Request.post(apiName,postData).then(res=>{
      let majorList = res.data;
      majorList = _.map(majorList,major=>{
        major = new MajorTypeForm(major);
        return major;
      });

      pagination.totalCount = res.rows;
      return majorList;
    });
  }

  static addMajor(majorForm){
    const apiName = 'api/majortype/save';
    let postData = majorForm.getObject();

    return Request.post(apiName,postData).then(res=>{
      return res.msg;
    });
  }

  static updateMajor(majorForm){
    const apiName = 'api/majortype/update';
    let postData = majorForm.getObject();

    return Request.post(apiName,postData).then(res=>{
      return res.msg;
    });
  }
}
